﻿using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace UtfprLibrary.Windows.Forms.ComponentModel {
    /// <summary>
    /// Provides a type converter to convert object references to and from other representations.
    /// </summary>
    internal class UtfprBindingNavigatorConverter : ReferenceConverter {
        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="type"></param>
        public UtfprBindingNavigatorConverter(Type type) : base(type) { }

        /// <summary>
        /// The value is allowed just if the current navigator isn't in Master mode,
        /// the currentReference isn't the same as the value, and the value is of type UtfprBindingNavigator. 
        /// </summary>
        protected override bool IsValueAllowed(ITypeDescriptorContext context, object value) {
            try {
                bool isMaster = (context.Instance as UtfprBindingNavigatorBase).CurrentMode == UBNModeEnum.Master;
                bool isCurrentReference = context.Instance == value;
                bool isUtfprBindingNavigator = value is UtfprBindingNavigatorBase;
                return (!isMaster && !isCurrentReference && isUtfprBindingNavigator);
            } catch (Exception ex) {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
    }
}
